// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
/* 设置cookie,session跨域配置 */
axios.defaults.withCredentials=true;
/* 设置post请求体,请求格式为json*/
axios.defaults.headers.post['Content-Type'] = 'application/json'
/* 设置全局axios写法 */
Vue.prototype.$http = axios

Vue.use(ElementUI);
Vue.config.productionTip = false


/*定义一个全局指令*/
Vue.directive('power', {
   //钩子函数，el为当前绑定的元素，binding为绑定的参数
   inserted: function (el, binding) {
      //获取会话存储的权限信息
     const powerList  = sessionStorage.getItem('powerList');
      console.log(powerList);
      const arr = powerList.split(',');
      var is = false ;
      for(var i=0;i<arr.length;i++){
        if(arr[i]===binding.value){
          is = true;
        }
      }
      if (!is){
        el.style.display='none';
      }else{
        el.style.display='block';
      }

   }
})

/*定义axios的请求拦截器，解决token赋值问题*/
axios.interceptors.request.use(function (config){//请求拦截器
     //获取token
  const token = sessionStorage.getItem('token');
  //判断token是否为空
  if(token){
    config.headers.token = token;
    return config;
  }
  return config;

},function(error){//错误处理
   console.log(error);
}
)

/*定义axios的响应拦截器，统一处理500错误*/
axios.interceptors.response.use(function (response){
   if(response.data.code===500){
     app.$message.error(response.data.msg);
     //app.$router.push("/");
     return response;
   }
   return response;
})


/* eslint-disable no-new */
var app =new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
